<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

$post = array();
//Common::pr($getCategory);
$OTHER_DETAILS = FALSE;

?>

<script src="<?=MEDIA?>js/passwordStrengthMeter.js"></script>

<script type="text/javascript">
	$(document).ready(function(){

		$('#insName').autocomplete({
			type: 'POST',
			serviceUrl: '<?=site_url()?>main/getInstitutes'
		});
		
		$('#insAddress_4').autocomplete({
			type: 'POST',
			serviceUrl: '<?=site_url()?>main/getCities',
			minChars: 3
		});

		$("#frmRegistrationStep-3 input[title]").tooltip();

		$('#usrPassword').keyup(function(){$('#result').html(passwordStrength($('#usrPassword').val(),$('#usrEmail').val()))})
		$('#usrEmail').keyup(function(){$('#result').html(passwordStrength($('#usrPassword').val(),$('#usrEmail').val()))})

		// Smart Wizard
		$('#wizard').smartWizard({transitionEffect:'slide', onLeaveStep:leaveAStepCallback, onFinish:onFinishCallback, keyNavigation: false});

		function leaveAStepCallback(obj, context) {
			var step_num = obj.attr('rel');
			var isStepValid = true;

			if (context.fromStep > context.toStep) {
				return true;
			} else {
				if($('#step-' + context.fromStep + '-message').length > 0) {
					$('#step-' + context.fromStep + '-message').remove();
				}
				if($('#step-' + context.toStep + '-message').length > 0) {
					$('#step-' + context.toStep + '-message').remove();
				}
			}

			if($('#frmRegistrationStep-' + step_num).length > 0) {
					$.ajax({
						type: 'POST',
						dataType: "json",
						url: '<?=site_url()?>main/registration_insert_update',
						data: $('#frmRegistrationStep-' + step_num).serialize(),
						beforeSend: function(xhr) {
							$('#modal').mask('<?=$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))?>');
						},
						success: function(data) {
							if(data.error) {
								isStepValid = false;

								if($('#step-' + step_num + '-message').length > 0) {
									$('#step-' + step_num + '-message').html(data.error);
									$('#step-' + step_num + '-message').show();
								} else {
									$('<div id="step-' + step_num + '-message" class="msgBox" style="width:696px; margin-bottom:20px;">' + data.error + '</div>').insertBefore($('#frmRegistrationStep-' + step_num));
									$('#step-' + step_num + '-message').show();
								}

								$('#wizard').smartWizard('setError',{stepnum:step_num,iserror:true});
								$('#wizard').smartWizard('showMessage','Please correct the errors!');
							} else {
								if($('#step-' + step_num + '-message').length > 0) {
									$('#step-' + step_num + '-message').remove();
								}
								$('#wizard').smartWizard('setError',{stepnum:step_num,iserror:false});
								if(step_num == 1) {

									// email tooltip
									if($("#frmRegistrationStep-2 #usrEmail").data('tooltip')) {
									//Save the current configuration.
									var conf = $("#frmRegistrationStep-2 #usrEmail").data('tooltip').getConf();
									$("#frmRegistrationStep-2 #usrEmail").removeData('tooltip').unbind();
									} else {
										var conf = new Object();
									}
									//Add the changed text.
									$("#frmRegistrationStep-2 #usrEmail").attr("title", $("#frmRegistrationStep-2 #usrEmail").data($('#regType').val() + '-title'));
									//Reapply the configuration.
									if($("#frmRegistrationStep-2 #usrEmail").attr("title"))
										$("#frmRegistrationStep-2 #usrEmail").tooltip(conf);

								}
							}
						},
						async: false
					})
					.always(function() {$('#body').unmask();});
			}
			return isStepValid;
		}

		function onFinishCallback() {
			var isValid = true;
			var data = 'finish=1';

			for(var step_num = 1; step_num <= 4; step_num ++) {
				if($('#frmRegistrationStep-' + step_num).length > 0) {
					data += '&' + $('#frmRegistrationStep-' + step_num).serialize();
				}
			}

			$.ajax({
				type: 'POST',
				dataType: "json",
				url: '<?=site_url()?>main/registration_insert_update',
				data: data,
				beforeSend: function(xhr) {
					$('#modal').mask('<?=$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))?>');
				},
				success: function(data) {
					if(data.error) {
						isStepValid = false;
						$('#wizard').smartWizard('setError',{stepnum:step_num,iserror:true});
						$('#wizard').smartWizard('showMessage','<?=$this->db->escape_str($this->lang->line("PLEASE_CORRECT_THE_ERRORS"))?>');
					} else {
						$.ajax({type: 'POST', url: '<?=site_url()?>main/registration_success', data: 'usrEmail=' + ((data.usrEmail)?(data.usrEmail):('')), beforeSend: function(xhr) {$('#modal').mask('<?=$this->db->escape_str($this->lang->line("PLEASE_WAIT,_PROCESSING_IN_PROGRESS"))?>');} }).
							always(function() {$('#body').unmask();});
					}
				},
				async: false
			})
			.always(function() {$('#body').unmask();});

			return isValid;

		}

	});
</script>

<div id="wizard" class="swMain">

	<div id="step-1" class="c">
		<h2 class="StepTitle"><?=$this->lang->line("REGISTRATION_TYPE")?></h2><br/>
		<?=form_open("", array("id" => "frmRegistrationStep-1", "autocomplete" => "off"))?>
		<?=form_hidden("step", 1)?>
		<div style="text-align:center; padding-top:70px;">
			<h2><?=$this->lang->line("NOT_REQUIRED_IF_YOU_HAVE_A_GMAIL_FACEBOOK_ACCOUNT")?></h2>
			<script type="text/javascript">
				function jsRegType(selected) {
					$('#regType').val(selected);
					$('#'+selected+'_button').removeClass('button');
					$('#'+selected+'_button').addClass('button_checked');
					$('#'+((selected == 'visitor')?('institute'):('visitor'))+'_button').removeClass('button_checked');
					$('#'+((selected == 'visitor')?('institute'):('visitor'))+'_button').addClass('button');
					if(selected == 'visitor') {
						$('#insNameContainer').hide();
					} else {
						$('#insNameContainer').show();
					}
				}
			</script>
			<input type="hidden" name="regType" id="regType" value="" />
			<?=form_input(array("class" => "button", "style" => "", "type" => "button", "id" => "institute_button", "value" => $this->lang->line("INSTITUTE_REGISTRATION"), "onclick" => "jsRegType('institute');"))?>
			<?=form_input(array("class" => "button", "style" => "", "type" => "button", "id" => "visitor_button", "value" => $this->lang->line("VISITOR_REGISTRATION"), "onclick" => "jsRegType('visitor');"))?>
		</div>
		<?=form_close()?>
	</div>

	<? $tabindex = 1; ?>

	<div id="step-2" class="c">
		<h2 class="StepTitle"><?=$this->lang->line("ACCOUNT_DETAILS")?></h2><br/>
		<?

			echo	form_open("", array("id" => "frmRegistrationStep-2", "autocomplete" => "off"));
			echo		form_hidden("step", 2);
			echo		form_input(array("name" => "usrEmail", "id" => "usrEmail", "title" => "", "data-institute-title" => $this->lang->line("INSTITUTE_EMAIL_ADDRESS_TOOLTIP"), "data-visitor-title" => $this->lang->line("VISITOR_EMAIL_ADDRESS_TOOLTIP"), "tabindex" => $tabindex, "class" => "form_data data w100", "style" => "width:510px; padding-right: 200px;", "value" => Common::val($post, "usrEmail"))).form_input(array("class" => "inlinebutton", "style" => "margin-left: -200px;", "type" => "button", "value" => $this->lang->line("E-MAIL_ADDRESS")))."<br/><br/>";
			$tabindex++;

			echo		form_password(array("name" => "usrPassword", "id" => "usrPassword", "tabindex" => $tabindex, "class" => "form_data data w100", "style" => "width:310px; padding-right: 200px;", "value" => "")).form_input(array("class" => "inlinebutton", "style" => "margin-left: -200px; width:200px; text-align:left;", "type" => "button", "value" => $this->lang->line("PASSWORD")))."<span style=\"color:green; width: 200px; text-align: center; display: inline-block; position:relative; top: 13px;\"><span style=\"display: inline-block; border-top:2px solid #82A2B2; border-left:2px solid #82A2B2; border-right:2px solid #82A2B2; width:160px; padding:5px;\">".$this->lang->line("PASSWORD_STRENGTH")."</span></span><br/><br/>";
			$tabindex++;

			echo		form_password(array("name" => "usrPasswordRe", "id" => "usrPasswordRe", "tabindex" => $tabindex, "class" => "form_data data w100", "style" => "width:310px; padding-right: 200px;", "value" => "")).form_input(array("class" => "inlinebutton", "style" => "margin-left: -200px; width:200px; text-align:left;", "type" => "button", "value" => $this->lang->line("RE-TYPE_PASSWORD")))."<span style=\"color:green; width: 200px; text-align: center; display: inline-block; position:relative; top: -13px;\"><span style=\"display: inline-block; border-bottom:2px solid #82A2B2; border-left:2px solid #82A2B2; border-right:2px solid #82A2B2; width:160px; padding:5px;\" id=\"result\">&nbsp;</span></span><br/><br/>";
			$tabindex++;

			echo	form_close();
		?>
	</div>

	<div id="step-3">
		<h2 class="StepTitle"><?=$this->lang->line("PROFILE_DETAILS")?></h2><br/>
		<?
			$options = array(0 => $this->lang->line("COUNTRY"));
			foreach($getCountry as $couData) {
				$options[$couData["couName"]] = $couData["couName"];
			}
			echo	form_open("", array("id" => "frmRegistrationStep-3", "autocomplete" => "off"));
			echo		form_hidden("step", 3);

			echo		"<div id=\"insNameContainer\">
							<fieldset class=\"form_data data\" style=\"padding: 10px; width:690px;\">
								<legend>Institute</legend>".
								form_input(array("name" => "insName", "id" => "insName", "title" => $this->lang->line("PLEASE_SELECT_THE_INSTITUTE_FROM_THE_LIST_IF_EXISTS"), "tabindex" => $tabindex, "class" => "form_data data w100", "style" => "width:485px; padding-right: 200px;", "value" => Common::val($post, "insName"))).form_input(array("class" => "inlinebutton", "style" => "margin-left: -200px;", "type" => "button", "value" => $this->lang->line("NAME_OF_THE_INSTITUTE")))."<br/><br/>".
								
								
								"<fieldset class=\"form_data data\" style=\"padding: 10px; width:670px;\">
									<legend>".$this->lang->line("ADDRESS")."</legend>
									".form_dropdown("insAddress_1", $options, NULL, "class=\"data\" style=\"width:250px;\" tabindex=\"".$tabindex."\"")."&nbsp;&nbsp;".
									form_input(array("name" => "insAddress_2", "id" => "insAddress_2", "tabindex" => $tabindex, "class" => "form_data data", "style" => "width:205px; padding-right: 200px;", "value" => Common::val($post, "insAddress_2"))).form_input(array("class" => "inlinebutton", "style" => "margin-left: -200px;", "type" => "button", "value" => $this->lang->line("STATE_PROVINCE")))."<br/><br/>".
									form_input(array("name" => "insAddress_3", "id" => "insAddress_3", "tabindex" => $tabindex, "class" => "form_data data", "style" => "width:140px; padding-right: 100px;", "value" => Common::val($post, "insAddress_3"))).form_input(array("class" => "inlinebutton", "style" => "margin-left: -100px;", "type" => "button", "value" => $this->lang->line("POSTAL_CODE")))."&nbsp;&nbsp;".
									form_input(array("name" => "insAddress_4", "id" => "insAddress_4", "tabindex" => $tabindex, "class" => "form_data data", "style" => "width:305px; padding-right: 100px;", "value" => Common::val($post, "insAddress_4"))).form_input(array("class" => "inlinebutton", "style" => "margin-left: -100px;", "type" => "button", "value" => $this->lang->line("CITY")))."<br/><br/>".
									form_input(array("name" => "insAddress", "id" => "insAddress", "tabindex" => $tabindex, "class" => "form_data data", "style" => "width:465px; padding-right: 200px;", "value" => Common::val($post, "insAddress"))).form_input(array("class" => "inlinebutton", "style" => "margin-left: -200px;", "type" => "button", "value" => $this->lang->line("ADDRESS")))."<br/>
								</fieldset><br/>".
								
								form_input(array("name" => "insWebsite", "id" => "insWebsite", "title" => $this->lang->line("NO_NEED_TO_SET_IT_IF_YOU_CHOOSED_AN_INSTITUTE_PREVIOUSLY"), "tabindex" => $tabindex, "class" => "form_data data w100", "style" => "width:485px; padding-right: 200px;", "value" => Common::val($post, "insWebsite"))).form_input(array("class" => "inlinebutton", "style" => "margin-left: -200px;", "type" => "button", "value" => $this->lang->line("WEBSITE")))."
							</fieldset>
						</div>";
			$tabindex++;

			if(count($getCategory)) {
				foreach($getCategory["USER"] as $catData) {
					if(in_array($catData["catType"], array("I", "C"))) {
						echo	Common::generateFormElement($catData, array("style" => "width:510px;", "tabindex" => $tabindex) + (($catData["catId"] == $this->config->item("categoryUSRFullname"))?(array("title" => $this->lang->line("REGISTRATION_FULL_NAME_EXPLANATION"))):(array())))."<br/><br/>";
						$tabindex++;
					} else {
						$OTHER_DETAILS = TRUE;
					}
				}
			}
		echo	form_close();
		?>
	</div>

<? if($OTHER_DETAILS) { ?>
	<div id="step-4">
		<h2 class="StepTitle"><?=$this->lang->line("OTHER_DETAILS")?></h2><br/>
		<?
			echo	form_open("", array("id" => "frmRegistrationStep-4", "autocomplete" => "off"));
			echo		form_hidden("step", 4);
			foreach($getCategory["USER"] as $catData) {
				if(!in_array($catData["catType"], array("I", "C"))) {
					echo		Common::generateFormElement($catData, array("style" => "width:510px;", "tabindex" => $tabindex))."<br/><br/>";
					$tabindex++;
				}
			}
			echo	form_close();
			?>
	</div>
	<? } ?>

	<ul>
		<li><a href="#step-1">
					<label class="stepNumber">1</label>
					<span class="stepDesc">
						<?=$this->lang->line("STEP")?> 1<br />
						<small><?=$this->lang->line("REGISTRATION_TYPE")?></small>
					</span>
			</a>
		</li>
		<li><a href="#step-2">
					<label class="stepNumber">2</label>
					<span class="stepDesc">
						<?=$this->lang->line("STEP")?> 2<br />
						<small><?=$this->lang->line("ACCOUNT_DETAILS")?></small>
					</span>
			</a>
		</li>
		<li><a href="#step-3">
					<label class="stepNumber">3</label>
					<span class="stepDesc">
						<?=$this->lang->line("STEP")?> 3<br />
						<small><?=$this->lang->line("PROFILE_DETAILS")?></small>
					</span>
			</a>
		</li>
		<? if($OTHER_DETAILS) { ?>
		<li><a href="#step-4">
					<label class="stepNumber">4</label>
					<span class="stepDesc">
						<?=$this->lang->line("STEP")?> 4<br />
						<small><?=$this->lang->line("OTHER_DETAILS")?></small>
					</span>
			</a>
		</li>
		<? } ?>
	</ul>

</div>
<br class="cb" />